﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using LPrisaMVC.Entities;

namespace LPrisaMVC.DAO
{
    public class CreditoDAO: MasterRepository, ICreditoDAO
    {
        public List<CREDITO> GetAllFromCreditos()
        {
            var query = from c in Context.CREDITOes.Include("CLIENTE")
                        orderby c.FechaCredito descending
                        select c;
            return query.ToList();
        }

        public List<CREDITO> GetFromCreditobyCliente(string cliente)
        {
            if (cliente != "")
            {
                var query = from v in Context.CREDITOes.Include("CLIENTE")
                            where v.CLIENTE.DniRuc.ToUpper().Contains(cliente.ToUpper()) ||
                                    v.CLIENTE.NombreRazonSocial.ToUpper().Contains(cliente.ToUpper())
                            select v;
                return query.ToList();
            }
            else
            {
                var query = from v in Context.CREDITOes.Include("CLIENTE")
                            select v;
                return query.ToList();
            }
        }

        public List<CREDITO> GetFromCreditobyFechaAndCliente(DateTime? fec1, DateTime? fec2, string cliente)
        {
            if (cliente != "")
            {
                var query = from h in Context.CREDITOes.Include("CLIENTE")
                            select h;

                if (fec1 != null && fec2 != null)//ambos
                {
                    query = from h in query
                            where (h.FechaCredito >= fec1.Value && h.FechaCredito <= fec2.Value) &&
                                    h.CLIENTE.NombreRazonSocial.ToUpper().Contains(cliente.ToUpper())
                            select h;
                }
                if (fec1 != null && fec2 == null)//solo cliente
                {
                    query = from h in query
                            where h.FechaCredito == fec1.Value &&
                                    h.CLIENTE.NombreRazonSocial.ToUpper().Contains(cliente.ToUpper())
                            select h;
                }
                else //retornar todos
                {
                    query = from h in query
                            select h;
                }
                return query.Take(500).ToList();
            }
            else
            {
                var query = from h in Context.CREDITOes.Include("CLIENTE")
                            select h;

                if (fec1 != null && fec2 != null)//solo fecha
                {
                    query = from h in query
                            where h.FechaCredito >= fec1.Value && h.FechaCredito <= fec2.Value
                            select h;
                }
                if (fec1 != null && fec2 == null)//solo cliente
                {
                    query = from h in query
                            where h.FechaCredito == fec1.Value
                            select h;
                }
                return query.Take(500).ToList();
            }
        }

        public CREDITO GetFromCreditoById(int id)
        {
            var query = from dv in Context.CREDITOes.Include("DETALLE_CREDITO.PRODUCTO").Include("DETALLEPAGOes")
                        where dv.Id.Equals(id)
                        select dv;
            return query.SingleOrDefault();
        }

        public void AddCredito(CREDITO credito)
        {
            foreach (var item in credito.DETALLE_CREDITO)
            {
                Context.Entry(item.PRODUCTO).State = EntityState.Unchanged;
                Context.Entry(item).State = EntityState.Added;
            }

            Context.Entry(credito.CLIENTE).State = EntityState.Unchanged;
            Context.CREDITOes.Add(credito);
            Context.SaveChanges();
        }

        public void UpdateCredito(CREDITO credito)
        {
            var estadosDetalle = Context.ChangeTracker.Entries<DETALLE_CREDITO>();

            Context.Entry(credito.CLIENTE).State = EntityState.Unchanged;
            Context.Entry(credito).State = EntityState.Modified;

            foreach (var item in credito.DETALLE_CREDITO)
            {
                Context.Entry(item.PRODUCTO).State = EntityState.Unchanged;

                Context.Entry(item).State = item.id == int.Parse("0") ? EntityState.Added : EntityState.Modified;

                item.ProductoId = item.PRODUCTO.Id;
                Context.Entry(item.PRODUCTO).State = EntityState.Detached;
            }

            Context.SaveChanges();
        }

        public void CancelarCredito(int id)
        {
            throw new NotImplementedException();
        }


        public bool GetAllCreditosByImpuestoIdOrClienteId(int idImpuesto, int idCliente)
        {
            var query = from c in Context.CREDITOes.Include("IMPUESTO").Include("CLIENTE")
                        where c.ImpuestoId.Equals(idImpuesto)
                        || c.ClienteId.Equals(idCliente)
                        select c;
            return query.Any();
        }
    
    }
}
